package com.maxbill.hadoop.reduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @功能
 * @作者 zuoshuai(MaxBill)
 * @日期 2017/11/17
 * @时间 14:46
 * @备注 Reduce
 */
public class MyReduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {

    List<String> textList = null;

    public MyReduce() {
        textList = new ArrayList<>();
        textList.add("孙权");
        textList.add("姜维");
    }

    /**
     * @作者 zuoshuai(MaxBill)
     * @日期 2017/11/17
     * @时间 14:46
     */
    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
        int sum = 0;
        while (values.hasNext()) {
            sum += values.next().get();
        }
        output.collect(key, new IntWritable(sum));
        String keyStr = new String(key.toString());
        boolean isHas = textList.contains(keyStr);
        if (isHas) {
            System.out.println(">>>>>" + keyStr + " [" + sum + "]");
        }
    }

}